<?php

	// Session
	session_start();
	
	// Nese vizitori eshte i identifikuar
	if (!isset($_SESSION['user_id'])) {
		header("Location: login.php");
	}
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	// Project
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/members/kendfavorits.html');
	define('UID',$_SESSION['user_id']);
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	
	
	// Connection
	dbOpen("comunity");
	
	
	// Inicializimi i shablonave
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../members"));
	$tmp->setContent("MYMENU",$tmp->menu("mymenu","libfavorits.php"));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	$tmp->setContent("MYCOMMENT",$tmp->menu("myfavorits",FILENAME));
	
	
	// DELETING
	if (isset($_POST['checkbox'])) {
	
		// Fshirja e titujve nga arkiva library
		foreach ($_POST['checkbox'] as $key) {
			
			// Delete row from DB
			mysql_query("DELETE FROM bookmarks WHERE id = '".$key."'");
		}
		
		// Kalo ne listimin e librave dhe trego porosin per librat e fshire
		header("Location: libfavorits.php?splash=dropfavorit");
		exit;
	}
	
	
	// SPLASH SCREEN
	if (isset($_GET['splash'])) 
		splash($_GET['splash'], "../members/kendfavorites.php");
	
	
	
	// HELP
	$tmp->setContent("HELP",$tmp->xmlContent("favorit","help.xml"));
	$tmp->setContent("DROP",$tmp->xmlContent("dropfavorits","help.xml"));
	
	
	$query = mysql_query("SELECT id,book_id,bookmark,completed
						  FROM bookmarks WHERE user_id = '".UID."' "); 
	
	$totalrows = mysql_num_rows($query);
	
	if ($totalrows > 0) {
		
		// Fshij bllokun per porosi boshe
		$MESSAGE = array();
		
		// Krijo fushen me te dhenat e komenteve
		while ($row = mysql_fetch_assoc($query)) {
			
			if ($row['bookmark']) {
			
				$book = mysql_fetch_assoc(mysql_query("SELECT volume,title, author_id FROM library WHERE id = '".$row['book_id']."'"));
				$row['volume'] = $book['volume'];
				$row['title'] = $book['title'];
				$row['author'] = implode(mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE id = '".$book['author_id']."'")));
				$row['completed'] = ($row['completed'] == 1) ? "|yes|" : "|no|";
				$row['totalrows'] = mysql_num_rows($query);
	
				// Fusha asociative per convertim te shenjezimeve ne shabllon
				$COMMENTS[] = $row;
			}
			else 
				// Fshij bllokun per porosi boshe
				$COMMENTS = array();
		}
	}
	else {
		
		// Defino porosin per librin akoma te pakomentuar
		$MESSAGE = $tmp->xmlContent('nofavorits');
		
		// Fshij bllokun per porosi boshe
		$COMMENTS = array();
	}
	
	
	// MESSAGE
	$tmp->setContent("MESSAGE",$MESSAGE);
	
	
	// COMMENTS
	$tmp->setContent("FORM",$COMMENTS);
	$tmp->setContent(array("totalrows"=>$totalrows));
	
	// LABELS
	$tmp->setLabels();
	

	// Dhe cdo gje ka nje fund, trego faqen						  
	echo $tmp->vorlage;

?>